cmake_minimum_required(VERSION 3.5)
SET(CMAKE_CXX_STANDARD 14)

message("Current Cmake version is : " ${CMAKE_VERSION})
project(testOptICP)

########################
# set compile options  #
########################
#delte the cache to rebuild after changing the options

OPTION(BUILD_WITH_OPENCV "Build with OpenCV2 for Image related Processing" ON)
OPTION(DEBUG_ON "Turn on DEBUG Mode" ON)

SET(CMAKE_CXX_FLAGS "-O3 -std=c++14 -w -fopenmp -pthread")
SET(CMAKE_C_FLAGS "-O3 -std=c++14 -fopenmp -pthread")

IF(DEBUG_ON)
MESSAGE("[Debug Mode]")
SET(CMAKE_BUILD_TYPE "Debug") 
SET(CMAKE_CXX_FLAGS_DEBUG "$ENV{CXXFLAGS} -O0 -Wall -g -ggdb")
ELSE()
MESSAGE("[Release Mode]")
SET(CMAKE_BUILD_TYPE "Release") 
SET(CMAKE_CXX_FLAGS_RELEASE "$ENV{CXXFLAGS} -O3 -Wall -g ") 
ENDIF()

# set(EXECUTABLE_OUTPUT_PATH ${PROJECT_SOURCE_DIR}/bin)

########################
# find libs & include  #
########################

find_package(OpenMP)
if (OPENMP_FOUND)
  set (CMAKE_C_FLAGS "${CMAKE_C_FLAGS} ${OpenMP_C_FLAGS}")
  set (CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} ${OpenMP_CXX_FLAGS}")
endif()

find_package(Eigen3 REQUIRED)
find_package(PCL REQUIRED)
find_package(yaml-cpp REQUIRED)
find_package(Ceres REQUIRED)
find_package(GTSAM REQUIRED QUIET)

#OpenCV2
IF(BUILD_WITH_OPENCV)
FIND_PACKAGE(OpenCV REQUIRED QUIET)
IF(OPENCV_FOUND) 
INCLUDE_DIRECTORIES(${OpenCV_INCLUDE_DIRS})
MESSAGE("OPENCV [OK]: " ${OpenCV_INCLUDE_DIRS}) 
ADD_DEFINITIONS(-DOPENCV_ON)
ENDIF(OPENCV_FOUND)
ENDIF(BUILD_WITH_OPENCV)

list(APPEND CMAKE_MODULE_PATH ${PROJECT_SOURCE_DIR}/cmake)
find_package(G2O REQUIRED) 
# g2o
set(G2O_INCLUDE_DIRS /usr/local/include/g2o)
set(G2O_LIB_DIR  /usr/local/lib/) 
SET(G2O_LIBRARIES g2o_cli g2o_core g2o_csparse_extension g2o_ext_freeglut_minimal 
             g2o_incremental g2o_interactive g2o_interface g2o_opengl_helper 
             g2o_parser g2o_simulator g2o_solver_cholmod g2o_solver_csparse 
             g2o_solver_dense g2o_solver_eigen g2o_solver_pcg g2o_solver_slam2d_linear 
             g2o_solver_structure_only g2o_stuff g2o_types_data g2o_types_icp 
             g2o_types_sba g2o_types_sclam2d g2o_types_sim3 g2o_types_slam2d
             g2o_types_slam2d_addons g2o_types_slam3d g2o_types_slam3d_addons)
IF(G2O_FOUND)
    message("G2O is found:" ${G2O_INCLUDE_DIRS}) 
    message("G2O lib:" ${G2O_LIBRARIES})
ENDIF(G2O_FOUND)

# include folder
include_directories(
        ${yaml-cpp_INCLUDE_DIRS}
        ${G2O_INCLUDE_DIRS}
        ${EIGEN3_INCLUDE_DIR}
        ${PCL_INCLUDE_DIRS}
        ${CERES_INCLUDE_DIRS}
        ${GTSAM_INCLUDE_DIR}
        ${PROJECT_SOURCE_DIR}/src
        ${PROJECT_SOURCE_DIR}/app)

# 用于指定链接器在查找库文件时需要搜索的路径。添加搜索路径
link_directories( ${CERES_LIBRARY_DIRS} ${G2O_LIB_DIR} ${GTSAM_LIBRARY_DIRS})

# source folder
file(GLOB_RECURSE CPP_LIST "${PROJECT_SOURCE_DIR}/src/*.cpp")

set(SRC_LIST  ${CPP_LIST} )
MESSAGE("SRC_LIST is " ${SRC_LIST})

########################
#      link libs       #
########################
#link pcl lib (neccessary)
SET(DEP_LIBS ${DEP_LIBS} ${PCL_LIBRARIES} ${CERES_LIBRARIES} ${G2O_LIBRARIES} yaml-cpp gtsam)

#link opencv (optional)
IF(BUILD_WITH_OPENCV)  
SET(DEP_LIBS ${DEP_LIBS} ${OpenCV_LIBS})
ENDIF(BUILD_WITH_OPENCV)

########################
#     add 3rdparty     #
########################

OPTION(TEST_ALL "Turn on all reg Mode" ON)

IF(TEST_ALL)
MESSAGE("[Build 3rdparty]")

# KCP
find_package(Threads REQUIRED)
find_package(nanoflann REQUIRED QUIET)
find_package(teaserpp REQUIRED QUIET)
set(TEASER_LIBRARIES teaserpp::teaser_registration)
add_subdirectory(3rdparty/KCP/kcp)

# fast_gicp
add_subdirectory(3rdparty/fast_gicp)

SET(DEP_LIBS ${DEP_LIBS} fast_gicp KCP::kcp)

ENDIF()

########################
#   add executables    #
########################

# 单独测试
add_executable(align_one app/align_one.cpp ${SRC_LIST})
target_link_libraries(align_one ${DEP_LIBS})

# 全部测试
IF(TEST_ALL)
MESSAGE("[Build align_all]")

add_executable(align_all app/align_all.cpp ${SRC_LIST})
target_link_libraries(align_all ${DEP_LIBS})

add_dependencies(align_one fast_gicp)
add_dependencies(align_all fast_gicp)

ENDIF()

